package com.ler.auth.service;

import cn.hutool.core.lang.Assert;
import com.ler.auth.entity.UserDO;
import com.ler.yrmall.member.api.MemberApi;
import com.ler.yrmall.member.dto.MemberDTO;
import com.ler.yrmall.basic.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author leron
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @DubboReference
    public MemberApi memberApi;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Result<MemberDTO> result = memberApi.getMemberByUsername(username);
        Assert.isTrue(result.isSuccess());
        UserDO user = new UserDO();
        List<GrantedAuthority> roleAll = AuthorityUtils.createAuthorityList("YURUO_MEMBER");
        BeanUtils.copyProperties(result.getData(),user);
        user.setAuthorityList(roleAll);
        return user;
    }

}
